﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using micsun.Logic;
using micsun.Logic.Security;
using System.Data;

namespace tmsWeb.Sys
{
    public partial class SysUsersMtn : micsun.Web.SecurityPage
    {
        private LogicArgs args = new LogicArgs();
        private commfuncs.SYS.User usr = new commfuncs.SYS.User();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.Params["Action"] != null)
                {
                    args.Parameters.Add("Action", Request.Params["Action"]);
                    SaveKey.Text = "<input name='Action' type='hidden' value=" + Request.Params["Action"].ToString() + " />";
                }
                else
                    throw (new NoPermissionException());
                if (Request.Params["USRID"] != null)
                {
                    args.Parameters.Add("USRID", Request.Params["USRID"]);
                    SaveKey.Text += "<input name='USRID' type='hidden' value='" + Request.Params["USRID"].ToString() + "' />";
                }
                else
                    throw (new NoPermissionException());
                if (Request.Params["Position"] != null)
                {
                    args.Parameters.Add("Position", Request.Params["Position"]);
                    SaveKey.Text += "<input name='Position' type='hidden' value='" + Request.Params["Position"].ToString() + "' />";
                }
                else
                    throw (new NoPermissionException());
            }
            catch
            {
                throw (new NoPermissionException());
            }

            if (!IsPostBack)
            {
                SetGrp();
                SetControls();
                SetData();
            }
            if (IsPostBack)
            {
                if (txtID.Text != "")
                {
                    args.Parameters["USRID"] = txtID.Text.ToString();
                }
                else
                {
                    args.Parameters["USRID"] = Request.Form["USRID"].ToString();
                }
            }
        }
        private void SetGrp()
        {
            DataTable dt = usr.GetDataTable("select GRPID,GRPDESC from ISMGRP");
            ddlGrp.DataSource = dt;
            ddlGrp.DataValueField = "GRPID";
            ddlGrp.DataTextField = "GRPDESC";
            ddlGrp.DataBind();
        }

        void SetControls()
        {
            string op = Request.Form["Action"].ToString().ToLower();
            switch (op)
            {
                case "new"://新增
                    lblTitle.Text = "用户管理——新增页面";
                    commfuncs.Misc.setTextEnabled(true, form);
                    commfuncs.Misc.clearText(form);
                    btn_OK.Text = "新增";
                    btn_OK.CausesValidation = true;
                    break;
                case "edt"://修改
                    lblTitle.Text = "用户管理——修改页面";
                    commfuncs.Misc.setTextEnabled(true, form);
                    txtID.Enabled = false;
                    btn_OK.Text = "修改";
                    btn_OK.CausesValidation = true;
                    IDRequired1.Enabled = false;
                    IDRequired2.Enabled = false;
                    break;
                case "qry"://查询
                    lblTitle.Text = "用户管理——查询页面";
                    commfuncs.Misc.setTextEnabled(false, form);
                    btn_OK.Visible = false;
                    btn_Cancel.Text = "返回";
                    break;
                case "del"://删除
                    lblTitle.Text = "用户管理——删除页面";
                    string js = "return confirm('是否删除？')";
                    btn_OK.Attributes["onclick"] = js;
                    btn_OK.CausesValidation = false;
                    commfuncs.Misc.setTextEnabled(false, form);
                    btn_OK.Text = "删除";
                    break;
                default://无权限
                    break;

            }
            //if (!(args.Parameters["Action"].ToString().ToLower().Equals("addbtn") || args.Parameters["Action"].ToString().ToLower().Equals("qrybtn")))
            //{
            //    LogicResults lrs = RecordLock.LockRecordByStringKey(args, "部门属性表", "单位编号");
            //    if (!lrs.IsSuccessful) HG304.Mds.Web.Transfer.ShowErrorPage("锁定记录失败！");
            //}
        }

        private void SetData()
        {
            if (!args.Parameters["Action"].ToString().ToLower().Equals("new"))
            {
                usr.USERID = args.Parameters["USRID"].ToString();
                usr.Select(usr.USERID);
                txtID.Text = usr.USERID;
                txtName.Text = usr.USERNAME;
                txtPSW.Text = txtREPSW.Text = "";
                ddlGrp.SelectedValue = usr.GRPID;
                if (this.User.ID == txtID.Text && args.Parameters["Action"].ToString().ToLower().Equals("edt"))
                {
                    string s = "alert('已成功修改当前用户“" + this.User.UserName + "”的数据，将自动登出系统！')";
                    btn_OK.Attributes["onclick"] = s;
                }
            }
        }

        private bool CheckData(string op)
        {
            bool res = true;
            switch (op)
            {
                case "new":
                    if (usr.IsHaveSameID(txtID.Text))
                    {
                        res = false;
                        AddMessage("用户编号不能重复，系统中已存在“" + txtID.Text + "”的用户编号。");
                        return res;
                    }
                    break;
               
                default:
                    break;
            }
            return res;
        }

        protected void btn_OK_Click(object sender, EventArgs e)
        {
            string op = Request.Form["Action"].ToString().ToLower();
            if (!CheckData(op))
            { return; }
            usr.USERID = txtID.Text;
            usr.Select(usr.USERID);
            usr.USERNAME = txtName.Text;
            bool IsChange = false;
            if (txtPSW.Text.Length != 0)
            {
                if (txtPSW.Text == txtREPSW.Text)
                {
                    usr.PASSWORD = txtPSW.Text;
                    IsChange = true;
                }
                else
                {
                    AddMessage("用户密码必须和确认密码一致！");
                    return;
                }
            }
            usr.LASTMODUSER = this.User.UserName;
            usr.LASTMODDATE = DateTime.Now;

            usr.GRPID = ddlGrp.SelectedValue;

            switch (op)
            {
                
                case "new"://新增
                    if (usr.Add())
                    {
                        Transfer.Redirect("SYS/SysUsers.aspx?USRID=" + args.Parameters["USRID"]);
                    }
                    else
                    {
                        AddMessage("新增失败");
                    }
                    break;
                case "edt"://修改
                    if (usr.Update(IsChange))
                    {
                        if (this.User.ID == txtID.Text)
                        {
                            Transfer.Redirect("Logout.aspx");
                        }
                        else
                        {
                            Transfer.Redirect("SYS/SysUsers.aspx?USRID=" + args.Parameters["USRID"] + "&Position=" + args.Parameters["Position"]);
                        }
                    }
                    else
                    {
                        AddMessage("修改失败");
                    }
                    break;
                case "qry"://查询
                    break;
                case "del"://删除
                    if (this.User.ID == txtID.Text)
                    {
                        AddMessage("用户编号为“" + this.User.ID + "”的用户，正在登录中，不允许删除！");
                    }
                    else
                    {
                        if (usr.Delete())
                        {
                            Transfer.Redirect("SYS/SysUsers.aspx?USRID=" + args.Parameters["USRID"] + "&Position=" + args.Parameters["Position"]);
                        }
                        else
                        {
                            AddMessage("删除失败");
                        }
                    }
                    break;
                default://无权限
                    break;
            }
        }

        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            Transfer.Redirect("SYS/SysUsers.aspx?USRID=" + args.Parameters["USRID"] + "&Position=" + args.Parameters["Position"]);
        }
    }
}
